package com.biz.primus.ms.auth.service;

import com.biz.primus.ms.base.service.AbstractBaseService;
import javax.transaction.Transactional;

import com.biz.primus.model.sso.account.vo.UserReqVo;
import com.biz.primus.ms.auth.dao.po.User;
import com.biz.primus.ms.auth.dao.repository.UserRepository;
import com.biz.primus.ms.auth.dao.specification.UserDynamicSpecification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class UserService extends AbstractBaseService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public Page<User> listUser(UserReqVo userReqVo) {
        return userRepository.findAll(new UserDynamicSpecification(userReqVo),
                new PageRequest(userReqVo.getPage() - 1, userReqVo.getPageSize()));
    }

    public User getUser(Long id) {
        return id == null ? null : userRepository.findOne(id);
    }

    public User getUserResVoById(String userUnionId){
        return userRepository.findByUserUnionId(userUnionId);
    }


    @Transactional
    public void createUser(User user) {
        User existUser = user;
        existUser.setId(idService.getNextId());
        userRepository.save(existUser);
    }
}